package com.promotores;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.promotores.jsonparsing.JSONReader;
import com.promotores.jsonparsing.JSONParser;
import com.promotores.jsonparsing.JSONReader2;
import com.promotores.jsonparsing.JSONReaderListener;
import com.promotores.util.Parametros;

public class JsonTipoEvento {
	//url to make request
	//private static String url = "http://200.16.7.112:3000/promoter_management/promoters/administradores_por_tipo_evento/1/2012-03-03.json";
	private static String url = "http://200.16.7.112:3000/promoter_management/promotions/event_types/";
	
	// JSON Node names
	//private static final String TAG_ADM_TIPOEVENTO = "administadores_por_tipo_evento";
	private static final String TAG_TIPOEVENTO = "event_type";	
	private static final String TAG_CREATED_AT = "created_at";
	private static final String TAG_TIPO = "tipo";
	private static final String TAG_ID = "id";
	private static final String TAG_UPDATED_AT = "updated_at";	
	
	// contacts JSONArray
	JSONArray tipoeventos = null;
	
	public List<BeanTipoEvento> read(Context context){
		List<BeanTipoEvento> lstTipoEventos = new ArrayList<BeanTipoEvento>();

		// Creating JSON Parser instance
		//JSONParser jParser = new JSONParser();
		String read = url+Parametros.getLast_update()+"(yyyy-MM-dd'T'HH:mm:ss'Z').json";
		//String readValues = jParser.read(read);

		JSONReader2 jReader = new JSONReader2();
		jReader.setJSONReaderListener((JSONReaderListener)context);
		try {
			//JSONArray jsonArray = new JSONArray(readValues);
			JSONArray jsonArray =(jReader.execute(read)).get();
			
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);			
							
				// Storing each json item in variable				
				int id = jsonObject.getJSONObject(TAG_TIPOEVENTO).getInt(TAG_ID);
				String tipo = jsonObject.getJSONObject(TAG_TIPOEVENTO).getString(TAG_TIPO);
				String created_at = jsonObject.getJSONObject(TAG_TIPOEVENTO).getString(TAG_CREATED_AT);
				String updated_at = jsonObject.getJSONObject(TAG_TIPOEVENTO).getString(TAG_UPDATED_AT);
								
				BeanTipoEvento tipoEvento = new BeanTipoEvento();
				tipoEvento.setId(id);
				tipoEvento.setTipoEvento(tipo);
				tipoEvento.setCreated_at(created_at);
				tipoEvento.setUpdated_at(updated_at);
				lstTipoEventos.add(tipoEvento);
			}
		} catch (Exception e) {
			System.out.println("Json: Exception");
			e.printStackTrace();
			return null;
		}
		return lstTipoEventos;
	}
	
	
	public List<BeanTipoEvento> dameSubTiposEventos(int idCategoria){
		JSONArray jTiposEvento;
		
		try {
			jTiposEvento=(new JSONReader().execute("http://200.16.7.112:3000/event_management/events/dameTipoEventos/"+String.valueOf(idCategoria) +".json")).get();
		} catch (InterruptedException e1) {
			e1.printStackTrace();
			return null;
		} catch (ExecutionException e1) {
			e1.printStackTrace();
			return null;
		}

		List<BeanTipoEvento> lstTipo= new ArrayList<BeanTipoEvento>();
		try{
			for(int i=0;i < jTiposEvento.length();i++){
			
				BeanTipoEvento objTipo = new BeanTipoEvento();
				objTipo.setId(jTiposEvento.getJSONObject(i).getInt("id"));
				objTipo.setTipoEvento(jTiposEvento.getJSONObject(i).getString("tipo"));
				//objTipo.setCategoria(idCategoria);

				lstTipo.add(objTipo);
				
			}	
			return lstTipo;
		}catch(JSONException e){
			Log.e("log_tag", "Error parsing data "+e.toString());
			return null;
		}
	}
}
